home *** CD-ROM | disk | FTP | other *** search
/ Acorn Risc Technologies StrongARM CD-ROM / Acorn Risc Technologies StrongARM CD-ROM.iso / ftp / documents / appnotes / 246_260 / 256c / Text
Encoding:
Text File  |  1994-05-19  |  9.1 KB  |  286 lines

  1. -----------------------------------------------------------------------------
  2. 7th April 1994
  3. -----------------------------------------------------------------------------
  4. Support Group Application Note
  5. Number: 256
  6. Issue: 1.00
  7. Author:JR/JB
  8. -----------------------------------------------------------------------------
  9.  
  10. Writing FIQ Code on RISC OS 3.5
  11.  
  12. -----------------------------------------------------------------------------
  13. Applicable Hardware: 
  14.                     The Risc PC range of computers
  15.  
  16. Related Application Notes:
  17.                           None
  18.  
  19. -----------------------------------------------------------------------------
  20. Copyright (C) 1994 Acorn Computers Limited 
  21.  
  22. Every effort has been made to ensure that the information in this leaflet is 
  23. true and correct at the time of printing. However, the products described in
  24. this leaflet are subject to continuous development and improvements and
  25. Acorn Computers Limited reserves the right to change its specifications at
  26. any time. Acorn Computers Limited cannot accept liability for any loss or
  27. damage arising from the use of any information or particulars in this
  28. leaflet. ACORN, ECONET and ARCHIMEDES are trademarks of Acorn Computers
  29. Limited.
  30. -----------------------------------------------------------------------------
  31. Support Group
  32. Acorn Computers Limited
  33. Acorn House
  34. Vision Park
  35. Histon
  36. Cambridge
  37. CB4 4AE                                                  
  38. -----------------------------------------------------------------------------
  39.  
  40. Introduction
  41.  
  42. For the majority of cases, Risc PC will run the ARM 610/700 in 26-bit mode 
  43. for compatibility with previous ARM processors and the software written for 
  44. them.  However, FIQ routines are entered with the ARM in 32-bit configuration.  
  45. Programmers need to take the following notes into account when converting or 
  46. writing FIQ code to run on a Risc PC.
  47.  
  48. The FIQ routine itself
  49.  
  50. Your FIQ routine will be entered in with the ARM in 32-bit configuration. In 
  51. this configuration any instructions with the PC as destination and the S bit 
  52. set will do the operation as specified and move the saved PSR to the current 
  53. PSR. The effect this has is that these instructions:
  54.  
  55. TEQP, TSTP, CMPP, CMNP
  56.  
  57. become useless as the results get discarded and the mode drops back into 
  58. whatever mode was interrupted to do the FIQ. These instructions have been 
  59. defined to be not necessarily supported in future ARMs when running in 32-bit 
  60. mode. These instructions:
  61.  
  62. <dataop><cc>S   pc, <reg>, <other>      (eg SUBS pc,lr,#4)
  63. LDM<cc><dba>    <reg>, {<with PC>}^     (eg LDMIA {r0-r3 ,pc}^)
  64.  
  65. can only sensibly be used as returns from interrupt.
  66.  
  67. ** do not enable interrupts in 32bit mode **
  68.  
  69. RISC OS 3.50 does not preserve the 32 bit status when returning from IRQs 
  70. or other interrupts (eg SWI). This means that a piece of code executing in 
  71. 32 bit mode with IRQs enabled may, at any time, drop into 26 bit mode. 
  72. Normally direct flag manipulation in 32-bit mode would be achieved using MRS 
  73. and MSR instructions, but for your purposes you are unlikely to need or want 
  74. to do this in a FIQ routine due to the amount of programming effort this 
  75. would entail.
  76.  
  77. What does all this mean? 
  78.  
  79. - subtractions in 32 bit mode will tend to not preserve flags
  80.  
  81. - returning from FIQ would be achieved using SUBS pc,lr,#4
  82.  
  83.  
  84. Installing your FIQ routine
  85.  
  86.  
  87. The ARM processor prohibits direct poking of the processor vectors whilst 
  88. in 26 bit mode. This is reasonable as it catches almost certainly OS-damaging 
  89. 26-bit code before it does any harm (in other words, it forces the authors 
  90. of such code to rewrite the code in the light of the fact that this code will 
  91. now be entered in 32-bit mode, not 26-bit mode as before).  As FIQ routines in 
  92. RISC OS tend to be very small, they also tend to need no conversion for the 
  93. ARM600.  Hence, to help with compatibility, RISC OS will detect a write to 
  94. location &1C (the FIQ vector) and allow this to happen.  Other writes to the 
  95. processor vectors are left as exceptions.  If you need rapid FIQ vector updates 
  96. then your code must be altered to switch to 32 bit mode whilst doing the FIQ 
  97. vector update.  ADFS does this and the relevant section of code is shown 
  98. below :-
  99.  
  100.                ; Switch to _32 mode with IRQs and FIQs off
  101.         ; Note must switch interrupts off before switching mode as
  102.         ; there can be an interrupt after the msr instruction
  103.         ; but before the following instruction.
  104.         ; For non-32-bit processors this section reads:
  105.         ; NOP
  106.         ; Push "r1"
  107.         ; ORR   r1, r1, #number
  108.         ; NOP
  109.         ; ORR   r1, r1, #number
  110.         ; NOP
  111.  
  112.         mrs     AL, r1, CPSR_all
  113.         Push    "r1"
  114.         ORR     r1, r1, #I32_bit :OR: F32_bit
  115.         msr     AL, CPSR_all, r1
  116.         ORR     r1, r1, #2_10000
  117.         msr     AL, CPSR_all, r1
  118.  
  119.         NOP
  120.         MOV     LR, #FiqVector          ; FIQ vector address
  121.  
  122.         ; Copy handler
  123. 40      LDR     R1, [R0], #4            ; Get opcode
  124.         TEQS    R1, #0                  ; All done?
  125.         STRNE   R1, [LR], #4            ; No then copy to FIQ area
  126.         BNE     %BT40                   ; And repeat
  127.  
  128.         ; And switch back - this bit reads as follows for non-32-bit processors:
  129.         ; Pull  "r1"
  130.         ; NOP
  131.         Pull    "r1"
  132.         msr     AL, CPSR_all, r1
  133.  
  134. *******  and from the headers: ******************
  135.  
  136. ; ARM6 PSR transfer macros
  137.  
  138. ; Condition code symbols
  139.  
  140. Cond_EQ *       0  :SHL: 28
  141. Cond_NE *       1  :SHL: 28
  142. Cond_CS *       2  :SHL: 28
  143. Cond_HS * Cond_CS
  144. Cond_CC *       3  :SHL: 28
  145. Cond_LO * Cond_CC
  146. Cond_MI *       4  :SHL: 28
  147. Cond_PL *       5  :SHL: 28
  148. Cond_VS *       6  :SHL: 28
  149. Cond_VC *       7  :SHL: 28
  150. Cond_HI *       8  :SHL: 28
  151. Cond_LS *       9  :SHL: 28
  152. Cond_GE *       10 :SHL: 28
  153. Cond_LT *       11 :SHL: 28
  154. Cond_GT *       12 :SHL: 28
  155. Cond_LE *       13 :SHL: 28
  156. Cond_AL *       14 :SHL: 28
  157. Cond_   * Cond_AL
  158. Cond_NV *       15 :SHL: 28
  159.  
  160. ; New positions of I and F bits in 32-bit PSR
  161.  
  162. I32_bit *       1 :SHL: 7
  163. F32_bit *       1 :SHL: 6
  164. IF32_26Shift *  26-6
  165.  
  166. ; Processor mode numbers
  167.  
  168. USR26_mode      *       2_00000
  169. FIQ26_mode      *       2_00001
  170. IRQ26_mode      *       2_00010
  171. SVC26_mode      *       2_00011
  172. USR32_mode      *       2_10000
  173. FIQ32_mode      *       2_10001
  174. IRQ32_mode      *       2_10010
  175. SVC32_mode      *       2_10011
  176. ABT32_mode      *       2_10111
  177. UND32_mode      *       2_11011
  178.  
  179. ; New register names
  180.  
  181. r13_abort       RN      13
  182. r14_abort       RN      14
  183. lr_abort        RN      14
  184.  
  185. r13_undef       RN      13
  186. r14_undef       RN      14
  187. lr_undef        RN      14
  188.  
  189.         MACRO
  190.         mrs     $cond, $rd, $psrs
  191.         LCLA    psrtype
  192. psrtype SETA    -1
  193.  [ "$psrs" = "CPSR" :LOR: "$psrs" = "CPSR_all"
  194. psrtype SETA    0 :SHL: 22
  195.  ]
  196.  [ "$psrs" = "SPSR" :LOR: "$psrs" = "SPSR_all"
  197. psrtype SETA    1 :SHL: 22
  198.  ]
  199.         ASSERT  psrtype <> -1
  200.         ASSERT  $rd <> 15
  201.         &       Cond_$cond :OR: 2_00000001000011110000000000000000 :OR: psrtype :OR: ($rd :SHL: 12)
  202.         MEND
  203.  
  204.         MACRO
  205.         msr     $cond, $psrl, $op2a, $op2b
  206.         LCLA    psrtype
  207.         LCLS    op2as
  208.         LCLA    op
  209.         LCLA    shift
  210. psrtype SETA    -1
  211.  [ "$psrl" = "CPSR" :LOR: "$psrl" = "CPSR_all"
  212. psrtype SETA    (0:SHL:22) :OR: (1:SHL:19) :OR: (1:SHL:16)
  213.  ]
  214.  [ "$psrl" = "CPSR_flg"
  215. psrtype SETA    (0:SHL:22) :OR: (1:SHL:19) :OR: (0:SHL:16)
  216.  ]
  217.  [ "$psrl" = "CPSR_ctl"
  218. psrtype SETA    (0:SHL:22) :OR: (0:SHL:19) :OR: (1:SHL:16)
  219.  ]
  220.  [ "$psrl" = "SPSR" :LOR: "$psrl" = "SPSR_all"
  221. psrtype SETA    (1:SHL:22) :OR: (1:SHL:19) :OR: (1:SHL:16)
  222.  ]
  223.  [ "$psrl" = "SPSR_flg"
  224. psrtype SETA    (1:SHL:22) :OR: (1:SHL:19) :OR: (0:SHL:16)
  225.  ]
  226.  [ "$psrl" = "SPSR_ctl"
  227. psrtype SETA    (1:SHL:22) :OR: (0:SHL:19) :OR: (1:SHL:16)
  228.  ]
  229.         ASSERT  psrtype <> -1
  230.  [ ("$op2a" :LEFT: 1) = "#"
  231.  ; Immediate operand
  232.  
  233. op2as   SETS    "$op2a" :RIGHT: ((:LEN: "$op2a")-1)
  234. op      SETA    $op2as
  235.  
  236.   [ "$op2b" = ""
  237.   ; Rotate not specified in immediate operand
  238. shift   SETA    0
  239.         WHILE   (op :AND: &FFFFFF00)<>0 :LAND: shift<16
  240. op      SETA    ((op:SHR:30):AND:3):OR:(op:SHL:2)
  241. shift   SETA    shift + 1
  242.         WEND
  243.         ASSERT  (op :AND: &FFFFFF00)=0
  244.   |
  245.   ; Rotate of immediate operand specified explicitly
  246.         ASSERT  (($op2b):AND:&FFFFFFE1)=0
  247. shift   SETA    ($opt2b):SHR:1
  248.   ]
  249. op      SETA    (shift :SHL: 8) :OR: op :OR: (1:SHL:25)
  250.  |
  251.  
  252.  ; Not an immediate operand
  253.   [ "$op2b" = ""
  254.   ; Unshifted register
  255. op      SETA    ($op2a) :OR: (0:SHL:25)
  256.   |
  257.         ! 1, "Shifted register not yet implemented in this macro!"
  258.   ]
  259.  ]
  260.         &       Cond_$cond :OR: 2_00000001001000001111000000000000 :OR: op :OR: psrtype
  261.         MEND
  262.  
  263. ; SetMode newmode, reg1, regoldpsr
  264. ;
  265. ; Sets processor mode to constant value newmode
  266. ; using register reg1 as a temporary.
  267. ; If regoldpsr is specified, then this register
  268. ; on exit holds the old PSR before the mode change
  269. ; reg1 on exit always holds the new PSR after the mode change
  270.  
  271.         MACRO
  272.         SetMode $newmode, $reg1, $regoldpsr
  273.  [ "$regoldpsr"=""
  274.         mrs     AL, $reg1, CPSR_all
  275.         BIC     $reg1, $reg1, #&1F
  276.         ORR     $reg1, $reg1, #$newmode
  277.         msr     AL, CPSR_all, $reg1
  278.  |
  279.         mrs     AL, $regoldpsr, CPSR_all
  280.         BIC     $reg1, $regoldpsr, #&1F
  281.         ORR     $reg1, $reg1, #$newmode
  282.         msr     AL, CPSR_all, $reg1
  283.  ]
  284.         MEND
  285.  
  286.